package com.mcsyr.clearitem;

import org.bukkit.Bukkit;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.ArrayList;
import java.util.List;

public class Dustbin {
    public static ItemStack PrevButton;
    public static ItemStack NextButton;
    public static List<Inventory> Dustbins = new ArrayList<>();

    /**
     * 初始化公共垃圾箱
     */
    public static void init() {
        //  清理旧的垃圾箱
        for (Inventory inv : Dustbins) {
            inv.clear();
        }
        //  清空垃圾箱数组
        Dustbins.clear();
        //  初始化垃圾箱
        for (int i = 0 ; i < Main.PublicDustbinPageSize ; i++) {
            Dustbins.add(Bukkit.createInventory(null, 54, Main.PublicDustbinName + i));
        }
        setDustbinButton();
    }

    /**
     * 把物品放进公共垃圾箱
     *
     * @param itemStack 需要放进垃圾箱的物品
     * @return 返回true表示成功放进垃圾箱, false表示失败，垃圾箱已满或者是白名单物品
     */
    public static Boolean addItem(ItemStack itemStack) {
        //  检查是否属于白名单类型的物品
        if (tools.isIncludedString(Main.PublicDustbinWhiteList, itemStack.getType().name())) {
            return false;
        }
        //  检查是否属于白名单名称的物品
        if (tools.isIncludedString(Main.PublicDustbinWhiteListName, itemStack.getItemMeta().getDisplayName())) {
            return false;
        }
        Main.DustbinLock = true;
        for (Inventory dustbin : Dustbins) {
            ItemStack[] contents = dustbin.getContents();
            for (int i = 0; i < contents.length; i++) {
                if (contents[i] != null && contents[i].isSimilar(itemStack)) {
                    int remainingQuantity = contents[i].getMaxStackSize() - contents[i].getAmount();
                    int itemStackAmount = itemStack.getAmount();
                    if (remainingQuantity <= 0) {
                        continue;
                    } else if (itemStackAmount > remainingQuantity) {
                        itemStack.setAmount(itemStackAmount - remainingQuantity);
                        contents[i].setAmount(contents[i].getAmount() + remainingQuantity);
                        dustbin.setItem(i, contents[i]);
                        continue;
                    } else {
                        contents[i].setAmount(contents[i].getAmount() + itemStackAmount);
                        dustbin.setItem(i, contents[i]);
                    }
                    Main.DustbinLock = false;
                    return true;
                } else if (contents[i] == null) {
                    dustbin.setItem(i, itemStack);
                    Main.DustbinLock = false;
                    return true;
                }
            }
        }
        Main.DustbinLock = false;
        return false;
    }

    /**
     * 清空公共垃圾箱
     * */
    public static void clearDustbin() {
        for (Inventory inv : Dustbins) {
            inv.clear();
        }
        setDustbinButton();
    }

    /**
     * 给垃圾箱添加翻页按钮
     * */
    public static void setDustbinButton() {
        //  设置上一页按钮物品数据
        PrevButton = new ItemStack(tools.getMaterial(Main.PublicDustbinPrevButtonType));
        ItemMeta itemMeta = PrevButton.getItemMeta();
        itemMeta.setDisplayName(Main.PublicDustbinPrevButtonName);
        PrevButton.setItemMeta(itemMeta);
        //  设置下一页按钮物品数据
        NextButton = new ItemStack(tools.getMaterial(Main.PublicDustbinNextButtonType));
        itemMeta = NextButton.getItemMeta();
        itemMeta.setDisplayName(Main.PublicDustbinNextButtonName);
        NextButton.setItemMeta(itemMeta);
        for (Inventory inv : Dustbins) {
            inv.setItem(45, PrevButton);
            inv.setItem(53, NextButton);
        }
    }
}
